//
//  BusinessActivity_ShaiXuan.m
//  HappyStore
//
//  Created by 点点充 on 16/1/19.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "BusinessActivity_ShaiXuan.h"

@interface BusinessActivity_ShaiXuan ()
{
    NSArray *aryHYTitle;//类型
    UIButton *btnHY;
    NSString *strHY;
    
    NSArray *aryPrice;//金额
    UIButton *btnPrice;
    NSString *strPrice;
    
    NSArray *aryCatege;//投资方式
    UIButton *btnCatege;
    NSString *strCatege;
    
    //地区
    UILabel *labAddress;
    NSString *provinceStr;
    NSString *cityStr;
    NSString *districtStr;
    
    
    SelectedAddress *selectAddress;
}
@end

@implementation BusinessActivity_ShaiXuan


- (void)viewDidLoad {
    [super viewDidLoad];
    
    aryHYTitle = @[@"互联网",@"金融",@"建筑地产",
                   @"能源矿业",@"交通运输",@"物流",
                   @"商务服务",@"教育培训",@"医疗卫生",
                   @"酒店餐饮",@"旅游度假",@"文化传媒",
                   @"休闲娱乐",@"商贸流通",@"农林牧渔",
                   @"生产制造",@"其他行业"];
    
    aryPrice = @[@"免费",@"0-100元",@"100-500元",@"500元以上"];
    aryCatege = @[@"个人",@"政府",@"企业"];
    
    UIBarButtonItem *rightBar = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(btnClickRight)];
    self.navigationItem.rightBarButtonItem = rightBar;
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    
}


/**
 *  @author LLJ, 16-01-16 17:01:06
 *
 *  rightBarClick
 */
- (void)btnClickRight
{
    //industry=根据行业&money=-根据金额，格式（0-5000000 亿以上：1000000000-亿）&type=根据类型（个人，企业，政府）&address= 根据地区（格式：省-市-区 如果没有就：省-无-无）
    NSMutableDictionary *dicInfo = [NSMutableDictionary dictionary];
    [dicInfo setObject:@"1" forKey:@"page"];
    if (strHY.length != 0) {//根据行业
        [dicInfo setObject:strHY forKey:@"industry"];
    }
    if (strPrice.length != 0) {//根据金额
        for (int i = 0; i<aryPrice.count; i++) {
            if ([strPrice isEqualToString:aryPrice[i]]) {
                //@"0-500万",@"500万-3000万",@"3000万-1亿",@"1亿以上"
                switch (i) {
                    case 0:
                    {
                        [dicInfo setObject:@"0-0" forKey:@"money"];
                    }
                        break;
                    case 1:
                    {
                        [dicInfo setObject:@"0-100" forKey:@"money"];
                    }
                        break;
                    case 2:
                    {
                        [dicInfo setObject:@"100-500" forKey:@"money"];
                    }
                        break;
                    case 3:
                    {
                        [dicInfo setObject:@"500-200000000" forKey:@"money"];
                    }
                        break;
                }
            }
        }
        
    }
    
    if (strCatege.length != 0) {//根据类型（个人，企业，政府）
        [dicInfo setObject:strCatege forKey:@"type"];
    }
    
    if (labAddress.text.length != 0) {//根据地区
        [dicInfo setObject:[NSString stringWithFormat:@"%@-%@-%@",provinceStr, cityStr, districtStr] forKey:@"address"];
    }
    
    
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"StartShaiXuanBusinessActivity" object:nil userInfo:dicInfo];
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
            return 230;
            break;
        case 1:
            return 100;
            break;
        default:
            return 65;
            break;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellShaiXuan = @"cellShaiXuan";//首页
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellShaiXuan forIndexPath:indexPath];;
    
    UILabel *labTitle = [cell viewWithTag:10];
    
    UILabel *labFenGe = [cell viewWithTag:11];
    labFenGe.translatesAutoresizingMaskIntoConstraints = YES;
    CGRect rect1 = labFenGe.frame;
    
    
    switch (indexPath.row) {
        case 0://行业
        {
            labTitle.text = @"行业:";
            rect1.origin.y = 229;
            labFenGe.frame = rect1;
            for (int i = 0; i<17; i++) {
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                [btn setTitle:aryHYTitle[i] forState:UIControlStateNormal];
                btn.frame = (CGRect){58+85*(i%3),18+33*(i/3),75,25};
                [btn setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
                btn.titleLabel.font = [UIFont systemFontOfSize:13.0f];
                
                [btn addTarget:self action:@selector(btnClickHangYe:) forControlEvents:UIControlEventTouchUpInside];
                btn.layer.masksToBounds = YES;
                btn.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
                btn.layer.borderWidth = 1.0f;
                btn.layer.cornerRadius = 5.0f;
                btn.tag = 20 +i;
                
                [cell addSubview:btn];
            }
            
        }
            break;
        case 1://金额
        {
            labTitle.text = @"金额:";
            rect1.origin.y = 99;
            labFenGe.frame = rect1;
            for (int i = 0; i<aryPrice.count; i++) {
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                [btn setTitle:aryPrice[i] forState:UIControlStateNormal];
                
                [btn setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
                btn.titleLabel.font = [UIFont systemFontOfSize:13.0f];
                
                [btn addTarget:self action:@selector(btnClickPrice:) forControlEvents:UIControlEventTouchUpInside];
                btn.layer.masksToBounds = YES;
                btn.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
                btn.layer.borderWidth = 1.0f;
                btn.layer.cornerRadius = 5.0f;
                btn.tag = 50 +i;
                
                switch (i) {
                    case 0:
                    {
                        btn.frame = (CGRect){58,18,65,25};
                    }
                        break;
                    case 1:
                    {
                        btn.frame = (CGRect){58+65+10,18,80,25};
                    }
                        break;
                    case 2:
                    {
                        btn.frame = (CGRect){58+65+80+10*2,18,80,25};
                    }
                        break;
                    case 3:
                    {
                        btn.frame = (CGRect){58,18+33,75,25};
                    }
                        break;
                        
                    default:
                        break;
                }
                
                
                [cell addSubview:btn];
            }
            
        }
            break;
        case 2://类型
        {
            labTitle.text = @"类型:";
            rect1.origin.y = 64;
            labFenGe.frame = rect1;
            
            
            for (int i = 0; i<aryCatege.count; i++) {
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                [btn setTitle:aryCatege[i] forState:UIControlStateNormal];
                btn.frame = (CGRect){58+75*(i%3),18+33*(i/3),65,25};
                [btn setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
                btn.titleLabel.font = [UIFont systemFontOfSize:13.0f];
                
                [btn addTarget:self action:@selector(btnClickCatege:) forControlEvents:UIControlEventTouchUpInside];
                btn.layer.masksToBounds = YES;
                btn.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
                btn.layer.borderWidth = 1.0f;
                btn.layer.cornerRadius = 5.0f;
                btn.tag = 60 +i;
                
                [cell addSubview:btn];
            }
            
            
        }
            break;
        case 3://地区
        {
            labTitle.text = @"地区:";
            rect1.origin.y = 64;
            labFenGe.frame = rect1;
            
            if (labAddress == nil) {
                labAddress = [[UILabel alloc] initWithFrame:(CGRect){58,15,246,31}];
                
                labAddress.layer.masksToBounds = YES;
                labAddress.layer.cornerRadius = 5.0f;
                labAddress.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
                labAddress.layer.borderWidth = 1.0f;
                labAddress.font = [UIFont systemFontOfSize:12.5f];
                labAddress.textColor = [UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f];
                
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                [btn setImage:[UIImage imageNamed:@"re_project_right"] forState:UIControlStateNormal];
                btn.frame = (CGRect){58,15.5,246,29.5};
                
                btn.imageEdgeInsets = UIEdgeInsetsMake(0, 210, 0, 0);
                [btn addTarget:self action:@selector(btnClickAddress) forControlEvents:UIControlEventTouchUpInside];
                
                [cell addSubview:labAddress];
                [cell addSubview:btn];
            }
            
        }
            break;
            
        default:
            break;
    }
    
    
    
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];//取消选中状态
    
}

#pragma mark - 选择行业
- (void)btnClickHangYe:(UIButton *)btn
{
    strHY = aryHYTitle[btn.tag-20];
    if (btn.selected) {
        strHY = @"";
        btn.selected = NO;
        [btn setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
        btn.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
    }else {
        //选中
        btn.selected = YES;
        [btn setTitleColor:[UIColor colorWithRed:255/255.0f green:107/255.0f blue:80/255.0f alpha:1.0f] forState:UIControlStateNormal];
        btn.layer.borderColor = [UIColor colorWithRed:255/255.0f green:107/255.0f blue:80/255.0f alpha:1.0f].CGColor;
        if (btnHY != nil) {
            btnHY.selected = NO;
            [btnHY setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
            btnHY.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
        }
        
        btnHY = btn;
    }
}

#pragma mark - 选择金额
- (void)btnClickPrice:(UIButton *)btn
{
    strPrice = aryPrice[btn.tag-50];
    if (btn.selected) {
        strPrice = @"";
        btn.selected = NO;
        [btn setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
        btn.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
    }else {
        //选中
        btn.selected = YES;
        [btn setTitleColor:[UIColor colorWithRed:255/255.0f green:107/255.0f blue:80/255.0f alpha:1.0f] forState:UIControlStateNormal];
        btn.layer.borderColor = [UIColor colorWithRed:255/255.0f green:107/255.0f blue:80/255.0f alpha:1.0f].CGColor;
        if (btnPrice != nil) {
            btnPrice.selected = NO;
            [btnPrice setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
            btnPrice.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
        }
        
        btnPrice = btn;
    }
}

#pragma mark - 选择类型
- (void)btnClickCatege:(UIButton *)btn
{
    strCatege = aryCatege[btn.tag-60];
    if (btn.selected) {
        strCatege = @"";
        btn.selected = NO;
        [btn setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
        btn.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
    }else {
        //选中
        btn.selected = YES;
        [btn setTitleColor:[UIColor colorWithRed:255/255.0f green:107/255.0f blue:80/255.0f alpha:1.0f] forState:UIControlStateNormal];
        btn.layer.borderColor = [UIColor colorWithRed:255/255.0f green:107/255.0f blue:80/255.0f alpha:1.0f].CGColor;
        if (btnCatege != nil) {
            btnCatege.selected = NO;
            [btnCatege setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
            btnCatege.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
        }
        
        btnCatege = btn;
    }
}

#pragma mark - 选择地区
- (void)btnClickAddress
{
    selectAddress = [SelectedAddress sharedSelectedAddress];
    selectAddress.delegatee = self;
    [selectAddress btnClickAddress];
}

- (void)seceltedRowSshowMsg:(NSString *)showMsg andProvince:(NSString *)strProvince andCity:(NSString *)strCity andDistrict:(NSString *)strDistrict
{
    labAddress.text = showMsg;
    provinceStr = strProvince;
    cityStr = strCity;
    districtStr = strDistrict;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
